﻿using System;

namespace JTLeigh.Commerce.Spencer.Core.Common.Services
{

	/// <summary>
	/// An <see cref="IExceptionHandlingService"/> implementation that uses Enterprise Library's 
	/// exception handling application block to handle exceptions.
	/// </summary>
	public class ExceptionHandlingService : IExceptionHandlingService
	{

		#region IExceptionHandlingService Members

		/// <summary>
		/// Handles the specified exception and returns a value indicating whether or not it should
		/// be rethrown.
		/// </summary>
		/// <param name="tenantId">Ignored.</param>
		/// <param name="exception">The <see cref="Exception"/> to handle.</param>
		/// <param name="exceptionPolicyKey">The exception policy key <see cref="String"/> used to
		/// identify the exception policy to use.</param>
		/// <returns><see langword="true"/> if the exception should be rethrown, 
		/// <see langword="false"/> otherwise.</returns>
		/// <remarks>
		/// <para>
		/// Most consumers should ignore the return value of this function and always rethrow the
		/// exception.  The few consumers that should honor the return value are those that can 
		/// simply swallow the exception without resulting in unexpected or unwanted behavior.
		/// </para>
		/// </remarks>
		public bool HandleException(Guid tenantId, Exception exception, string exceptionPolicyKey)
		{
            Safety.AssertArgumentNotNull("exception", exception);
            Safety.AssertArgumentNotNull("exceptionPolicyKey", exceptionPolicyKey);

			return Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.ExceptionPolicy.HandleException(exception, exceptionPolicyKey);
		}

		#endregion // IExceptionHandlingService Members

	}

}
